<?php namespace Common\Model;
use Hdphp\Model\Model;
class Goods extends Model{
	protected $table='goods';
	protected $validate=array(
	array('gname','required','商品名称不能为空',3,3),
//	array('marketprice','required','市场价不能为空',3,3),
//	array('shopprice','reqiured','商城价不能为空',3,3)
	);
	//自动完成
	protected $auto=array(
	array('time','time','function',3,1)
	);
	//添加的方法
	public function store(){
		//商品基本信息添加
		if(!$this->create()) return false;
//		$_POST['pic']=
		$this->data['pic']=implode(',',$_POST['pic']);
//		p($this->data);die;
		$gid=$this->add($this->data);
		//商品属性表添加
		$attr=array();
		$spec=array();
		$attrModel=new \Common\Model\GoodsAttr;
		
		foreach ($_POST['attr'] as $k=>$v) {
			$attr['gtvalue']=$v;
			$attr['taid']=$k;
			$attr['gid']=$gid;
			$attrModel->add($attr);
			
		}
		
		foreach ($_POST['spec'] as  $ke=>$va) {
			foreach ($va['size'] as $k=>$val) {
				$spec['gtvalue']=$va['size'][$k];
				$spec['gid']=$gid;
				$spec['taid']=$ke;
				$spec['added']=$va['more'][$k];
				$attrModel->add($spec);
			}
		}
		//商品详情表的添加
		$de=array();
		$detail= new \Common\Model\Detail;
		$de['gid']=$gid;
		$de['intro']=$_POST['intro'];
		$de['server']=$_POST['server'];
		$news=array();
		foreach($_POST['big'] as $v){
			$de['big']=implode(',', $_POST['big']);
			$type=strrchr($v,'.');
			$newPaths=str_replace($type, 'small_'.$type, $v);
			$newPathm=str_replace($type, 'mid_'.$type, $v);
			Image::thumb($v,$newPathm,60,60,5);
			Image::thumb($v,$newPaths,45,45,5);
			$news['small'][]=$newPaths;
			$news['mid'][]=$newPathm;
			$de['small']=implode($news['small'], ',');
			$de['mid']=implode($news['mid'], ',');
		}
		//p($news);
		//p($de);die;
		$detail->add($de);		
		return true;
	}
	public function edit($gide){
		if(!$this->create()) return false;
		$attrModel=new \Common\Model\GoodsAttr;
		
//		p($_POST);
		//货品表修改
		$listModel=new \Common\Model\Goodslist;
		//获取旧的数据
		$listData=$listModel->where(array('gid'=>$gide))->get();
//		p($listData);
		$listData2=array();
		foreach ($listData as $key => $value) {
			$listData2[$value['glid']]['combine']=explode(',', $value['combine']);
		}
//		p($listData2);die;
		$oldList=array();
		foreach ($listData2 as $key => $value) {
			foreach($value['combine'] as $k=>$v){
				$mes=$attrModel->where(array('gid'=>$gide))->where(array('gtid'=>$v))->pluck('gtvalue');
				$oldList[$key]['combine'][$mes]=$v;
			}
			
		}
	
		//
	
		$attrModel=new \Common\Model\GoodsAttr;
		$attrModel->where(array('gid'=>$gide))->delete();
		$this->data['pic']=implode(',',$_POST['pic']);
//		p($this->data);die;
		$gid=$this->where(array('gid'=>$gide))->save($this->data);
		//商品属性表添加
		$attr=array();
		$spec=array();
		
		foreach ($_POST['attr'] as $k=>$v) {
//			p($_POST['attr']);
//			p($v);
				$attr['gtvalue']=$v['gtvalue'];
//				p($attr);
				$attr['taid']=$k;
				$attr['gid']=$gide;
//				p($attr);die;
				$attrModel->where(array('gid'=>$gide))->add($attr);
		}
//		
		
		foreach ($_POST['spec'] as  $ke=>$va) {
			foreach ($va['gtvalue'] as $k=>$val) {
				$spec['gtvalue']=$va['gtvalue'][$k];
				$spec['added']=$va['added'][$k];
				$spec['taid']=$ke;
				$spec['gid']=$gide;
				$attrModel->where(array('gid'=>$gide))->add($spec);
			}
		}
		//获取新的gtid数据
		$app=array();
		
		foreach($_POST['spec'] as $k=>$v){
			foreach ($v['gtvalue'] as $key => $value) {
				$app[$value]=$attrModel->where(array('gid'=>$gide))->where(array('gtvalue'=>$value))->pluck('gtid');
			}
		}
		$newData=array();
		foreach ($oldList as $k => $v) {
			foreach($v['combine'] as $ka=>$va){
				foreach($app as $ke=>$ve){
					if($ke==$ka){
						$newData[$k]['combine'][$ke]=$ve;
					}
				}
			}
		}
		foreach ($newData as $key => $value) {
			$newData[$key]['combine']=implode(',', $value['combine']);
		}
		foreach ($newData as $key => $value) {
			$listModel->where(array('gid'=>$gide))->where(array('glid'=>$key))->save(array('combine'=>$value['combine']));
		}
//		p($newData);die;
//		p($oldList);
//		p($app);
		
		//商品详情表的修改
		$de=array();
		$detail= new \Common\Model\Detail;
//		$de['gid']=$gid;
		$de['intro']=$_POST['intro'];
		$de['server']=$_POST['server'];
		$news=array();
		foreach($_POST['big'] as $v){
			$de['big']=implode(',', $_POST['big']);
			$type=strrchr($v,'.');
			$newPaths=str_replace($type, 'small_'.$type, $v);
			$newPathm=str_replace($type, 'mid_'.$type, $v);
			Image::thumb($v,$newPathm,60,60,5);
			Image::thumb($v,$newPaths,45,45,5);
			$news['small'][]=$newPaths;
			$news['mid'][]=$newPathm;
			$de['small']=implode($news['small'], ',');
			$de['mid']=implode($news['mid'], ',');
		}
		//p($news);
		//p($de);die;
		$detail->where(array('gid'=>$gide))->save($de);		

	
		return true;
	}	
}


 ?>